#light
open FSharp.Xml

let ccconfig = readFromFile @"C:\Users\robert\Documents\articles\Language Oriented Programming\ccnet.config"
let ccconfigString = writeToString ccconfig

//writeTreeToFile @"C:\Users\robert\Documents\articles\Language Oriented Programming\ccnet.config.fs" ccconfig

let project name queque workingDir trunkUrl username incExc build timeTrigger =
    let triggers = 
        match timeTrigger with 
        | Some time -> 
            elementSubElems "triggers" [] 
                [ emptyElement "scheduleTrigger" ["time", time; "buildCondition", "ForceBuild"] ]
        | _ -> empty
    let workingDirElem = textElement "workingDirectory" [] workingDir 
    let sourceControl =
        let svnElements provider =
            let providerTag = if provider then "sourceControlProvider" else "sourcecontrol"
            let urlUsername = [ textElement "trunkUrl" [] trunkUrl; textElement "username" [] username ; ]
            elementSubElems providerTag [ "type", "svn";] urlUsername        
        match incExc with
        | Some (incExcType, pattern) -> 
            let filter = element incExcType [] (element "pathFilter" [] (textElement "pattern" [] pattern))
            elementSubElems "sourcecontrol" [ "type", "filtered";]  [ svnElements false; filter]
        | None -> svnElements false
    let tasks =
        let msbuildTask = [ textElement "executable" [] "MSBuild"; textElement "buildArgs" [] build; ]
        element "tasks" [] (elementSubElems "exec" [] msbuildTask);
    let attribs = [ "name", name; "queue", queque;]
    elementSubElems "project" attribs [ triggers; workingDirElem; sourceControl; tasks ]

let branchBuilds name workingDir url username filter = 
    let queue = Printf.sprintf "MyProj.%s" name
    [ project (Printf.sprintf  "MyProj.%s.Continuous.framework" name) queue 
        workingDir url username 
        (Some("inclusionFilters", filter)) "/t:FRAMEWORK" None; 
      project (Printf.sprintf  "MyProj.%s.Continuous" name) queue 
        workingDir url username 
        (Some("exclusionFilters", filter)) "/t:QUICK" None; 
      project (Printf.sprintf  "MyProj.%s.Release" name) queue 
        workingDir url username 
        None "/t:TESTS" (Some "23:00"); ]

let all = branchBuilds "trunk" @"c:\source\MyProj" 
            "https://svnserver/repository/myproj/trunk/myproj" "rpickering" 
            "/myproj/trunk/myproj/framework/**/*.*"
        @ branchBuilds "branch1" @"c:\source\branch1" 
            "https://svnserver/repository/myproj/branches/branch1" "rpickering" 
            "/myproj/branches/branch1/framework/**/*.*"

let outfile =  @"C:\Users\robert\Documents\articles\Language Oriented Programming\testccnet.config"        
writeToFile outfile (element "cruisecontrol" [] (elementList all))
writeToFile "ccnet.config" (element "cruisecontrol" [] (elementList all))
(*
let thing =
element "cruisecontrol" [] ( elementList [    element "project" [ attrib "name" "MyProj.trunk.Continuous.framework"; attrib "queue" "MyProj.trunk";] (      elementList [        textElement "workingDirectory" [] "c:\source\MyProj"
;         element "sourcecontrol" [ attrib "type" "filtered";] (          elementList [            element "sourceControlProvider" [ attrib "type" "svn";] (              elementList [                textElement "trunkUrl" [] "https://svnserver/repository/myproj/trunk/myproj"
;                 textElement "username" [] "rpickering"
;               ]             )
;             element "inclusionFilters" [] (              element "pathFilter" [] (                textElement "pattern" [] "/myproj/trunk/myproj/framework/**/*.*"
              )
            )
;           ]         )
;         element "tasks" [] (          element "exec" [] (            elementList [              textElement "executable" [] "MSBuild"
;               textElement "buildArgs" [] "/t:FRAMEWORK"
;             ]           )
        )
;       ]     )
;     element "project" [ attrib "name" "MyProj.trunk.Continuous"; attrib "queue" "MyProj.trunk";] (      elementList [        textElement "workingDirectory" [] "c:\source\MyProj"
;         element "sourcecontrol" [ attrib "type" "filtered";] (          elementList [            element "sourceControlProvider" [ attrib "type" "svn";] (              elementList [                textElement "trunkUrl" [] "https://svnserver/repository/myproj/trunk/myproj"
;                 textElement "username" [] "rpickering"
;               ]             )
;             element "exclusionFilters" [] (              element "pathFilter" [] (                textElement "pattern" [] "/myproj/trunk/myproj/framework/**/*.*"
              )
            )
;           ]         )
;         element "tasks" [] (          element "exec" [] (            elementList [              textElement "executable" [] "MSBuild"
;               textElement "buildArgs" [] "/t:QUICK"
;             ]           )
        )
;       ]     )
;     element "project" [ attrib "name" "MyProj.trunk.Release"; attrib "queue" "MyProj.trunk";] (      elementList [        textElement "workingDirectory" [] "c:\source\MyProj"
;         element "sourcecontrol" [ attrib "type" "svn";] (          elementList [            textElement "trunkUrl" [] "https://svnserver/repository/myproj/trunk/myproj"
;             textElement "username" [] "rpickering"
;           ]         )
;         element "tasks" [] (          element "exec" [] (            elementList [              textElement "executable" [] "MSBuild"
;               textElement "buildArgs" [] "/t:ALL"
;             ]           )
        )
;         element "tasks" [] (          element "exec" [] (            elementList [              textElement "executable" [] "MSBuild"
;               textElement "buildArgs" [] "/t:TESTS"
;             ]           )
        )
;       ]     )
;     element "project" [ attrib "name" "MyProj.branch1.Continuous.framework"; attrib "queue" "MyProj.branch1";] (      elementList [        textElement "workingDirectory" [] "c:\source\mpbranch1"
;         element "sourcecontrol" [ attrib "type" "filtered";] (          elementList [            element "sourceControlProvider" [ attrib "type" "svn";] (              elementList [                textElement "trunkUrl" [] "https://svnserver/repository/myproj/branches/branch1"
;                 textElement "username" [] "rpickering"
;               ]             )
;             element "inclusionFilters" [] (              element "pathFilter" [] (                textElement "pattern" [] ">/myproj/branches/branch1/framework/**/*.*"
              )
            )
;           ]         )
;         element "tasks" [] (          element "exec" [] (            elementList [              textElement "executable" [] "MSBuild"
;               textElement "buildArgs" [] "/t:FRAMEWORK"
;             ]           )
        )
;       ]     )
;     element "project" [ attrib "name" "MyProj.branch1.Continuous"; attrib "queue" "MyProj.branch1";] (      elementList [        textElement "workingDirectory" [] "c:\source\mpbranch1"
;         element "sourcecontrol" [ attrib "type" "filtered";] (          elementList [            element "sourceControlProvider" [ attrib "type" "svn";] (              elementList [                textElement "trunkUrl" [] "https://svnserver/repository/myproj/branches/branch1"
;                 textElement "username" [] "rpickering"
;               ]             )
;             element "exclusionFilters" [] (              element "pathFilter" [] (                textElement "pattern" [] "/myproj/branches/branch1/framework/**/*.*"
              )
            )
;           ]         )
;         element "tasks" [] (          element "exec" [] (            elementList [              textElement "executable" [] "MSBuild"
;               textElement "buildArgs" [] "/t:QUICK"
;             ]           )
        )
;       ]     )
;     element "project" [ attrib "name" "MyProj.branch1.Release"; attrib "queue" "MyProj.branch1";] (      elementList [        textElement "workingDirectory" [] "c:\source\mpbranch1"
;         element "sourcecontrol" [ attrib "type" "svn";] (          elementList [            textElement "trunkUrl" [] "https://svnserver/repository/myproj/branches/branch1"
;             textElement "username" [] "rpickering"
;           ]         )
;         element "tasks" [] (          element "exec" [] (            elementList [              textElement "executable" [] "MSBuild"
;               textElement "buildArgs" [] "/t:ALL"
;             ]           )
        )
;         element "tasks" [] (          element "exec" [] (            elementList [              textElement "executable" [] "MSBuild"
;               textElement "buildArgs" [] "/t:TESTS"
;             ]           )
        )
;       ]     )
;   ] )
*)